import request from '@/utils/request'
import $ from 'jquery'

/**
 * 获取所有的雇员
 */
export function getEmployees(currentPage, pageSize, limit) {
  return request({
    url: '/employee/list.json?limit=' + limit,
    method: 'post',
    data: {
      'detailDepart': true,
      'pageQueryParam': {
        pageSize, currentPage
      }
    }
  })
}

export function getEmployeesByDepartment(departmentId) {
  return request({
    url: '/employee/list.json?limit=false',
    method: 'post',
    data: {
      departmentId
    }
  })
}

export function updateEmployee(employeeObj) {
  return request({
    url: '/employee/update.do',
    method: 'post',
    data: employeeObj
  })
}

export function addEmployee(employeeObj) {
  return request({
    url: '/employee/add.do',
    method: 'post',
    data: employeeObj
  })
}

export function deleteEmployee(employeeId) {
  return request({
    url: '/employee/delete.action',
    method: 'post',
    data: {
      employeeId: employeeId
    }
  })
}

export function exportExcel(filename) {

  const url = 'http://localhost:10003/api/employee/export.do?filename=' + filename
  const form = $('<form></form>').attr('action', url).attr('method', 'get')

  form.append($('<input/>').attr('type', 'hidden').attr('name', 'filename').attr('value', filename))
  form.appendTo('body').submit().remove()
}

// 生成图表等请求
export function fetchEmployeeDistributionChart() {
  return request({
    url: '/employee/genChart/employeeDistribution.action',
    method: 'get'
  })
}
